package com.ss.android.wenda.answer.list;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.a.a.NightMode;
import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.UIUtils;
import com.bytedance.frameworks.b.a.ComponentsManifest;
import com.bytedance.retrofit2.Callback;
import com.bytedance.retrofit2.ResponseWrapper;
import com.ss.android.account.Spipe;
import com.ss.android.article.base.app.AppData;
import com.ss.android.article.base.feature.detail2.config.DetailStyleConfig;
import com.ss.android.article.base.feature.detail2.widget.DetailTitleBar.OnFollowQuestionListener;
import com.ss.android.article.base.feature.detail2.widget.DetailTitleBar;
import com.ss.android.article.base.feature.detail2.widget.tagview.TagLayout;
import com.ss.android.article.base.ui.NoDataViewFactory$ImgType;
import com.ss.android.article.base.ui.aa;
import com.ss.android.article.base.ui.ab;
import com.ss.android.article.base.ui.ac;
import com.ss.android.article.base.ui.ad;
import com.ss.android.article.base.ui.NoNetView;
import com.ss.android.article.base.utils.JSONObjectWrapper;
import com.ss.android.article.browser.R;
import com.ss.android.article.common.page.PageList;
import com.ss.android.common.lib.MobClickCombiner;
import com.ss.android.common.callback.CallbackCenter;
import com.ss.android.common.callback.SSCallback;
import com.ss.android.common.util.json.JsonUtil;
import com.ss.android.newmedia.BaseAppData;
import com.ss.android.topic.d.a;
import com.ss.android.wenda.c.l;
import com.ss.android.wenda.c.p;
import com.ss.android.wenda.model.Answer;
import com.ss.android.wenda.model.Question;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

public final class AnswerListFragment extends a implements
        NightMode,
        Callback,
        DetailTitleBar.OnChildViewClickCallback,
        OnFollowQuestionListener,
        com.ss.android.wenda.model.Answer.Answer$a {
    private View A;
    private View B;
    private com.ss.android.wenda.answer.list.b C;
    private com.ss.android.wenda.answer.list.a D;
    private int E;
    private boolean F;
    private Resources G;
    private boolean H;
    private SSCallback I;
    private SSCallback J;
    private View.OnClickListener K;
    private SSCallback L;
    Question k;
    private Activity l;
    private DetailTitleBar m;
    private ViewGroup n;
    private NoNetView o;
    private NoNetView p;
    private String q;
    private String r;
    private String s;
    private String t;
    private com.ss.android.wenda.model.response.b u;
    private com.ss.android.wenda.a.a v;
    private View w;
    private com.ss.android.k.aa x;
    private com.ss.android.k.aa y;
    private View z;

    public AnswerListFragment() {
        this.E = 0;
        this.F = false;
        this.H = false;
        this.J = new com.ss.android.wenda.answer.list.d(this);
        this.K = new e(this);
        this.L = new com.ss.android.wenda.answer.list.g(this);
    }

    public final void onBackClick() {
        this.getActivity().onBackPressed();
    }

    public final void onTopMoreTitleClick() {
        if (this.u != null && this.u.c != null) {
            MobClickCombiner.onEvent(this.getActivity(), "question", "share_button");
            this.D = new com.ss.android.wenda.answer.list.a(this.getActivity(), this.k, "question");
            this.C = new com.ss.android.wenda.answer.list.b(this.getActivity(), this.D);
            this.C.show();
        }
    }

    public final void onCloseAllWebPageClick() {
    }

    public final void onWechatShareClick() {
    }

    public final void onQQShareClick() {
    }

    public final void onMomentShareClick() {
    }

    public final void onInfoBackClick() {
    }

    static int a(AnswerListFragment arg0, int arg1) {
        arg0.E = arg1;
        return arg1;
    }

    static com.ss.android.wenda.a.a a(AnswerListFragment arg1) {
        return arg1.v;
    }

    private static void a(String arg1) {
        if (Logger.debug()) {
            Logger.d("AnswerListFragment", arg1);
        }
    }

    private void a(boolean arg5) {
        if (this.u != null) {
            if (this.z == null) {
                this.z = android.support.design.a.b(this.a, R.layout.answer_list_bottom_layout);
            }

            if (arg5) {
                this.A = this.z.findViewById(com.ss.android.article.browser.R.id.fold_layout_top_divider_line);
                UIUtils.setVisibility(this.A, 0);
            }

            com.ss.android.topic.b.a v0 = this.e;
            View v1 = this.z;
            if (!v0.a.contains(v1)) {
                v0.a.add(0, v1);
                v0.notifyDataSetChanged();
            }

            UIUtils.setVisibility(this.z, 0);
            this.n();
            this.y.a(this.k);
            if (this.z == null) {
                return;
            }

            DetailStyleConfig.a(this.z.findViewById(com.ss.android.article.browser.R.id.folder_answer_tv_container));
            com.ss.android.article.base.feature.detail2.config.a.b(this.z.findViewById(com.ss.android.article.browser.R.id.fold_reason_layout));
        }
    }

    public final void onFollowQuestionBtnClick(View arg5) {
        if (!Spipe.inst().isLogin) {
            ComponentsManifest.findComponent(com.ss.android.account.v2.bb.class).startAccountLoginActivity(this.l);
        } else if (arg5.isSelected()) {
            android.support.design.a.a(this.q, 0, new i(this), this.t);
        } else {
            android.support.design.a.a(this.q, 1, new j(this), this.t);
        }
    }

    public final void a(AbsListView arg2, int arg3, int arg4, int arg5) {
        super.a(arg2, arg3, arg4, arg5);
        this.b("loadmore");
    }

    public final void onCacheResponse(ResponseWrapper response) {
        Throwable v6 = null;
        if (response != null) {
            Object v0 = response.body;
            a("onResponse");
            this.F = true;
            if (this.isViewValid()) {
                if (v0 == null) {
                    this.onErrorResponse(v6);
                } else {
                    if (((com.ss.android.wenda.model.response.b) v0).a == 67686) {
                        if (this.p == null) {
                            this.p = android.support.design.a.a(this.getActivity(), this.n, ac.b(NoDataViewFactory$ImgType.DELETE_ARTICLE), ad.a(this.getString(com.ss.android.article.browser.R.string.info_article_deleted)), ((ab) null));
                        }

                        this.c();
                        this.p.initNoDataActionBtn();
                        this.p.setVisibility(View.VISIBLE);
                    }

                    this.u = ((com.ss.android.wenda.model.response.b) v0);
                    this.k = ((com.ss.android.wenda.model.response.b) v0).c;
                    if (this.k == null) {
                        return;
                    }

                    this.m.setFollowQuestionBtnShow(false);
                    this.v.d = ((com.ss.android.wenda.model.response.b) v0).g;
                    if (this.k.mNiceAnswerCount <= 0) {
                        if (this.k.mNormalAnswerCount <= 0) {
                            this.b("enter_0");
                        } else {
                            this.b("enter_0_fold");
                        }
                    }

                    if (this.k != null && this.k.mShareData != null) {
                        this.k.mShareData.mShareSource = this.k.mQid;
                    }

                    PageList v1 = this.j();
                    com.ss.android.wenda.model.response.d v2 = new com.ss.android.wenda.model.response.d();
                    v2.a = ((com.ss.android.wenda.model.response.b) v0).a;
                    v2.b = ((com.ss.android.wenda.model.response.b) v0).b;
                    v2.e = ((com.ss.android.wenda.model.response.b) v0).f;
                    v2.d = ((com.ss.android.wenda.model.response.b) v0).e;
                    v2.c = ((com.ss.android.wenda.model.response.b) v0).d;
                    v1.onLoadCompleted(v2);
                    if (this.v != null && this.u.c != null) {
                        this.v.c = this.u.c.mNiceAnswerCount + this.u.c.mNormalAnswerCount;
                    }

                    UIUtils.setVisibility(this.p, 8);
                    this.b();
                    this.c();
                    this.m();
                    if (this.u.f) {
                        this.o();
                    } else if (this.v.getCount() > 0) {
                        this.o();
                        this.a(false);
                        return;
                    }

                    this.p();
                }
            }
        }
    }

    public final void onErrorResponse(Throwable throwable) {
        StringBuilder v1 = new StringBuilder("onErrorResponse, error = ");
        String v0 = throwable != null ? throwable.getMessage() : "unknown";
        a(v1.append(v0).toString());
        this.b("enter_api_fail");
        if (this.isViewValid()) {
            this.d();
        }
    }

    public final void onStartLoading(boolean arg3, boolean arg4) {
        a("onStartLoading, firstPage = " + arg3);
        super.onStartLoading(arg3, arg4);
        this.isViewValid();
    }

    static Question b(AnswerListFragment arg1) {
        return arg1.k;
    }

    private void b(String arg3) {
        if (!android.support.design.a.isTextEmpty(arg3)) {
            MobClickCombiner.onEvent(this.getActivity(), "question", arg3);
        }
    }

    public final void b() {
        super.b();
        UIUtils.setVisibility(this.o, 8);
    }

    public final void onFinishLoading(boolean arg3, boolean arg4) {
        a("onFinishLoading, firstPage = " + arg3);
        super.onFinishLoading(arg3, arg4);
        if ((this.isViewValid()) && !this.j().mHasMore) {
            this.b.hideFooterContent();
            this.a(false);
        }
    }

    static void c(AnswerListFragment arg0) {
        arg0.p();
    }

    static void d(AnswerListFragment arg0) {
        arg0.n();
    }

    public final void d() {
        if (this.o == null) {
            this.o = android.support.design.a.a(this.getActivity(), this.n, ac.b(NoDataViewFactory$ImgType.NOT_NETWORK), ad.a(this.getString(com.ss.android.article.browser.R.string.not_network_tip)), ab.a(new aa(this.getString(com.ss.android.article.browser.R.string.label_retry), this.K)));
        }

        this.c();
        this.o.initNoDataActionBtn();
        this.o.setVisibility(View.VISIBLE);
    }

    static com.ss.android.k.aa e(AnswerListFragment arg1) {
        return arg1.y;
    }

    static void f(AnswerListFragment arg0) {
        arg0.m();
    }

    public final void f() {
        a("refresh");
        this.e();
        HashMap v0 = new HashMap();
        ((Map) v0).put("qid", String.valueOf(this.q));
        if (!android.support.design.a.isTextEmpty(this.s)) {
            ((Map) v0).put("enter_from", this.s);
        }

        if (!android.support.design.a.isTextEmpty(this.t)) {
            ((Map) v0).put("api_param", this.t);
        }

        ((Map) v0).put("gd_ext_json", this.q());
        new com.ss.android.wenda.a.d(((Map) v0), ((Callback) this)).e();
    }

    static View g(AnswerListFragment arg1) {
        return arg1.w;
    }

    public final BaseAdapter g() {
        this.v = new com.ss.android.wenda.a.a(this.q, 1, this.r, this.t);
        this.registerLifeCycleMonitor(this.v);
        ((a) this).a.setRecyclerListener(this.v);
        return this.v;
    }

    static com.ss.android.wenda.model.response.b h(AnswerListFragment arg1) {
        return arg1.u;
    }

    public final PageList h() {
        return new com.ss.android.wenda.a.e(this.q, this.t, this.q());
    }

    static int i(AnswerListFragment arg1) {
        return arg1.E;
    }

    public final boolean i() {
        return false;
    }

    static void j(AnswerListFragment arg0) {
        arg0.k();
    }

    final void k() {
        if (this.u != null && this.k != null) {
            com.ss.android.wenda.model.response.b v0 = this.u;
            int v0_1 = v0.d == null || v0.d.size() <= 0 ? 0 : 1;
            if (v0_1 == 0) {
                if (this.k.mNormalAnswerCount <= 0) {
                    this.p();
                    if (this.B == null) {
                        this.B = LayoutInflater.from(this.getActivity()).inflate(com.ss.android.article.browser.R.layout.gl, this.a, false);
                    }

                    ViewGroup.LayoutParams v0_2 = this.B.getLayoutParams();
                    v0_2.height = Math.max(UIUtils.getScreenHeight(this.getActivity()) - this.w.getHeight() - this.m.getHeight() - 40,
                            ((int) TypedValue.applyDimension(1, 240f, this.getActivity().getResources().getDisplayMetrics())));
                    this.B.setLayoutParams(v0_2);
                    this.e.a(this.B);
                    return;
                }

                this.o();
                this.a(true);
                return;
            }

            this.o();
            if (this.k.mNormalAnswerCount <= 0) {
                this.p();
                return;
            }

            this.a(false);
        }
    }

    static Activity k(AnswerListFragment arg1) {
        return arg1.l;
    }

    static DetailTitleBar l(AnswerListFragment arg1) {
        return arg1.m;
    }

    public final void l() {
        this.v.notifyDataSetChanged();
    }

    private void m() {
        if (this.k != null) {
            if (this.w == null) {
                this.w = android.support.design.a.b(this.a, R.layout.answer_list_question_header_layout);
                this.e.a(this.w);
                this.w.getViewTreeObserver().addOnGlobalLayoutListener(new com.ss.android.wenda.answer.list.h(this));
            }

            com.ss.android.article.base.feature.detail2.config.a.a(this.w, this.l.getResources().getColor(com.ss.android.article.browser.R.color.ssxinmian4));
            if (this.x == null) {
                this.x = new com.ss.android.k.aa(this.w).a(com.ss.android.article.browser.R.id.question_title, new l()).a(com.ss.android.article.browser.R.id.question_desc, new l()).a(com.ss.android.article.browser.R.id.question_thumb_container, new p()).a(com.ss.android.article.browser.R.id.tag_layout, new l()).a(com.ss.android.article.browser.R.id.bottom_view, new l());
            }

            this.x.a(this.k);
        }
    }

    private void n() {
        if (this.y == null) {
            this.y = new com.ss.android.k.aa(this.z).a(com.ss.android.article.browser.R.id.folder_answer_tv,
                    new com.ss.android.wenda.c.i(this.r, this.t))
                    .a(com.ss.android.article.browser.R.id.fold_reason_layout, new com.ss.android.wenda.c.i(this.r, this.t))
                    .a(com.ss.android.article.browser.R.id.fold_reason_top_divider, new com.ss.android.wenda.c.i(this.r, this.t))
                    .a(com.ss.android.article.browser.R.id.folder_answer_tv_container, new com.ss.android.wenda.c.i(this.r, this.t));
        }
    }

    private void o() {
        this.e.b(this.B);
    }

    public final void onAddressEditClicked(View arg1) {
    }

    public final void onCreate(Bundle bundle) {
        String v1_1;
        this.l = this.getActivity();
        Intent v0 = this.l.getIntent();
        if (v0 != null) {
            this.r = v0.getStringExtra("gd_ext_json");
            this.t = v0.getStringExtra("api_param");
            this.s = JsonUtil.a(this.r, "enter_from");
            if (this.r != null) {
                try {
                    v1_1 = new JSONObject(this.r).optString("log_pb");
                    if (v1_1 == null) {
                    } else {
                        new JSONObject(v1_1);
                    }
                } catch (JSONException v1) {
                }
            }

            if (android.support.design.a.isTextEmpty(this.s)) {
                this.s = v0.getStringExtra("enter_from");
            }

            if (this.r != null) {
                try {
                    v1_1 = new JSONObject(this.r).optString("log_pb");
                    if (v1_1 == null) {
                    } else {
                        new JSONObject(v1_1);
                    }
                } catch (JSONException v1) {
                }
            }

            this.t = android.support.design.a.a(this.t, this.s, "question");
            this.q = v0.getStringExtra("qid");
        }

        a("onCreate, mQuestionId = " + this.q);
        try {
            Long.valueOf(this.q);
        } catch (Exception v0_1) {
            v0_1.printStackTrace();
        }

        if (android.support.design.a.isTextEmpty(this.q)) {
            this.l.finish();
        }

        AppData.getInstance();
        this.G = this.getResources();
        this.H = AppData.isNightMode();
        super.onCreate(bundle);
    }

    public final View onCreateView(LayoutInflater arg3, ViewGroup arg4, Bundle arg5) {
        this.n = (ViewGroup) super.onCreateView(arg3, arg4, arg5);
        android.support.design.a.a(this.getActivity(), this.n);
        Answer.registerListener(((com.ss.android.wenda.model.Answer.Answer$a) this));
        return this.n;
    }

    public final void onDestroy() {
        super.onDestroy();
        Answer.unregisterListener(((com.ss.android.wenda.model.Answer.Answer$a) this));
        CallbackCenter.removeCallback(BaseAppData.callbackTypeDeleteAnswer, this.I);
        CallbackCenter.removeCallback(BaseAppData.callbackTypeFontSizeChange, this.L);
        CallbackCenter.removeCallback(BaseAppData.callbackTypeDiggBuryChange, this.J);
        this.b("back");
        if (!this.F) {
            this.b("back_no_content");
        }
    }

    public final void onResume() {
        int v6 = R.color.ly;
        int v5 = R.color.gq;
        int v4 = R.color.ssxinmian4;
        super.onResume();
        if (this.H != AppData.isNightMode()) {
            this.H = AppData.isNightMode();
            if (!this.isDestroyed()) {
                AppData.isNightMode();
                if (this.m != null) {
                    this.m.init();
                }

                if (this.A != null) {
                    this.A.setBackgroundColor(this.G.getColor(com.ss.android.article.browser.R.color.ssxinxian1));
                }

                if (this.y != null && this.k != null) {
                    this.y.a(this.k);
                }

                if (this.z == null && this.a != null) {
                    this.z = android.support.design.a.b(this.a, R.layout.answer_list_bottom_layout);
                }

                if (this.z != null) {
                    this.z.findViewById(com.ss.android.article.browser.R.id.folder_answer_tv_container).setBackgroundDrawable(this.getResources().getDrawable(com.ss.android.article.browser.R.drawable.mian4));
                    this.z.findViewById(com.ss.android.article.browser.R.id.fold_reason_layout).setBackgroundColor(this.getResources().getColor(v4));
                    ((TextView) this.z.findViewById(com.ss.android.article.browser.R.id.folder_answer_tv)).setTextColor(this.getResources().getColor(com.ss.android.article.browser.R.color.ssxinzi3));
                    ((TextView) this.z.findViewById(com.ss.android.article.browser.R.id.fold_reason_tv)).setTextColor(this.getResources().getColor(com.ss.android.article.browser.R.color.update_comment_more_text_selector));
                }

                this.m();
                if (this.w != null) {
                    ((TagLayout) this.w.findViewById(com.ss.android.article.browser.R.id.tag_layout)).a();
                }

                if (this.a != null) {
                    this.a.setAdapter(this.e);
                }

                if (this.n != null) {
                    this.n.setBackgroundColor(this.getResources().getColor(com.ss.android.article.browser.R.color.ssxinmian3));
                }

                if (this.B != null) {
                    this.e.b(this.B);
                    this.B = LayoutInflater.from(this.getActivity()).inflate(com.ss.android.article.browser.R.layout.gl, this.a, false);
                    this.e.a(this.B);
                }

                if (this.b == null) {
                    return;
                }

                this.d.setBackgroundColor(this.G.getColor(v4));
                ((TextView) this.d.findViewById(com.ss.android.article.browser.R.id.ss_retry)).setTextColor(this.getResources().getColor(v6));
                ((TextView) this.d.findViewById(com.ss.android.article.browser.R.id.ss_text)).setTextColor(this.getResources().getColor(v6));
                if (this.b.mFooterTopDivider != null) {
                    this.b.mFooterTopDivider.setBackgroundResource(v5);
                }

                if (this.b.mFooterBottomDivider == null) {
                    return;
                }

                this.b.mFooterBottomDivider.setBackgroundResource(v5);
            }
        }
    }

    public final void onViewCreated(View view, Bundle bundle) {
        super.onViewCreated(view, bundle);
        CallbackCenter.addCallback(BaseAppData.callbackTypeFontSizeChange, this.L);
        this.m = ((AnswerListActivity) this.l).a;
        this.m.setOnChildViewClickCallback(((DetailTitleBar.OnChildViewClickCallback) this));
        this.m.setWdlogoShow(true);
        this.m.setOnFollowQuestionListener(((OnFollowQuestionListener) this));
        this.I = new f(this);
        CallbackCenter.addCallback(BaseAppData.callbackTypeDeleteAnswer, this.I);
        CallbackCenter.addCallback(BaseAppData.callbackTypeDiggBuryChange, this.J);
    }

    private void p() {
        if (this.z != null) {
            com.ss.android.topic.b.a v0 = this.e;
            if (v0.a.remove(this.z)) {
                v0.notifyDataSetChanged();
            }
        }
    }

    private String q() {
        JSONObjectWrapper v0 = new JSONObjectWrapper();
        v0.put("enter_from", "click_answer");
        if (!android.support.design.a.isTextEmpty(this.r)) {
            String v1 = JsonUtil.a(this.r, "enter_from");
            if (!android.support.design.a.isTextEmpty(v1)) {
                v0.put("parent_enterfrom", v1);
            }

            v1 = JsonUtil.a(this.r, "ansid");
            if (!android.support.design.a.isTextEmpty(v1)) {
                v0.put("enterfrom_answerid", v1);
            } else {
                v0.put("enterfrom_answerid", this.q);
            }

            v1 = JsonUtil.a(this.r, "log_pb");
            if (android.support.design.a.isTextEmpty(v1)) {
            } else {
                v0.put("log_pb", v1);
            }
        }

        return v0.jsonObject.toString();
    }

    @Override
    public void f(@NonNull Object o) {

    }
}

